unit uFrmTestePascalScript;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPSComponent, StdCtrls , uPSCompiler, FMTBcd, DB, Mask, DBCtrls,
  DBClient, Provider, SqlExpr , uPSUtils, uPSComponent_StdCtrls , uPSC_std,
   DBTables;

type
  TFrmTestePascalScript = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    PSScript1: TPSScript;
    SQLDataSet1: TSQLDataSet;
    DataSetProvider1: TDataSetProvider;
    ClientDataSet1: TClientDataSet;
    SQLDataSet1IDMOV: TIntegerField;
    ClientDataSet1IDMOV: TIntegerField;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    DataSource1: TDataSource;
    PSImport_StdCtrls1: TPSImport_StdCtrls;
    ComboBox1: TComboBox;
    Button2: TButton;
    Button3: TButton;
    procedure PSScript1Compile(Sender: TPSScript);
    procedure Mensagem ( Msg : string);
    procedure Button1Click(Sender: TObject);
    function CompileScript ( Script : AnsiString ; out ByteCode, Messages : AnsiString) : Boolean;
    function ExtendedCompiler ( Compiler : TPSPascalCompiler ; const Name : AnsiString) : Boolean;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTestePascalScript: TFrmTestePascalScript;

implementation

uses uDM;

{$R *.dfm}

procedure TFrmTestePascalScript.Button1Click(Sender: TObject);
var i : Integer;
begin
{  PSScript1.Script := Memo1.Lines;
  PSScript1.Compile;
  for i := 0 to PSScript1.CompilerMessageCount - 1 do
    Memo2.Lines.Add('COMPILER: ' + PSScript1.CompilerMessages[i].MessageToString);
  PSScript1.Execute;  }



end;

procedure TFrmTestePascalScript.Button2Click(Sender: TObject);
begin
  DM.UniConnection1.GetTableNames(ComboBox1.Items,False,False);

end;

procedure TFrmTestePascalScript.Button3Click(Sender: TObject);
begin

end;

function TFrmTestePascalScript.CompileScript(Script: AnsiString; out ByteCode,
  Messages: AnsiString): Boolean;
var Compiler : TPSPascalCompiler;
    i : integer;
begin
  ByteCode := '';
  Messages := '';
  Compiler := TPSPascalCompiler.Create;
  //Compiler.OnUses := ExtendedCompiler;
  try
    Result := Compiler.Compile(Script) and Compiler.GetOutput(ByteCode);
    for i := 0 to Compiler.MsgCount - 1 do
      if Length(Messages) = 0 then
        Messages := Compiler.Msg[i].MessageToString
      else
        Messages := Messages + #13#10 + Compiler.Msg[i].MessageToString;
  finally
    Compiler.Free;
  end;
 ;
end;

function TFrmTestePascalScript.ExtendedCompiler(Compiler: TPSPascalCompiler;
  const Name: AnsiString): Boolean;
var CustomClass : TPSCompileTimeClass;
begin
  try
    Compiler.AddDelphiFunction('procedure Mensagem ( Msg : string);');
    SIRegisterTObject(Compiler);
    SIRegisterTComponent(Compiler);
    CustomClass := Compiler.AddClass(Compiler.FindClass('TCustomClientDataSet'),TClientDataSet);
    CustomClass.RegisterMethod('procedure First;');
    CustomClass.RegisterMethod('procedure Next;');
    CustomClass.RegisterPublishedProperties;
    Result := True;
  except
    Result := False;

  end;

end;

procedure TFrmTestePascalScript.Mensagem(Msg: string);
begin
  ShowMessage(msg);
end;

procedure TFrmTestePascalScript.PSScript1Compile(Sender: TPSScript);
begin
  Sender.AddMethod(Self,@TFrmTestePascalScript.Mensagem,'procedure Mensagem ( Msg : string);');

end;

end.
